const router = require('express').Router()
const U = { ...require('../../utils'), ...require('./utils') }
const Order = require('../../models/order')
const Food = require('../../models/food')
const User = require('../../models/user')

router.route('/')
  .all(U.abort_not_login)
  .get((req, res) => {
    console.log('获取商家' + req.session.shop.shop + '的所有订单信息')
    const raw_orders = Order.all_by_shop_id(req.session.shop.id)
    const detail_orders = raw_orders.map(o => {
      const food = Food.get(o.food_id)
      const user = User.get(o.user_id)
      return {...o,food_name: food.name,user_name: user.name}
    })
    res.json(U.Ok(detail_orders))
  })

module.exports = router